package com.qzdsoft.erpcloud.service.basic.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.qzdsoft.erpcloud.domain.basic.MeasurementUnit;
import com.qzdsoft.erpcloud.mapper.basic.MeasurementUnitMapper;
import com.qzdsoft.erpcloud.service.basic.MeasurementUnitService;
import com.qzdsoft.erpcloud.vo.query.basic.MeasurementUnitInfo;
import com.qzdsoft.utils.constant.SystemConstant;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.ResultEnum;

/**
 * Created by Admin
 */
@Service
public class  BasicMeasurementUnitServiceImpl implements MeasurementUnitService {

    @Autowired
    private MeasurementUnitMapper basicMeasurementUnitMapper;

    @Override
    public MeasurementUnit selectById(Integer id) {
        return basicMeasurementUnitMapper.selectByPrimaryKey(id);
    }

    @Override
    public Response<String> saveOrUpd(MeasurementUnit basicMeasurementUnit) {
        if(null == basicMeasurementUnit.getCompanyId()){
            basicMeasurementUnit.setCompanyId(SystemConstant.companyIdGg);
        }
        if(null != basicMeasurementUnit.getId()){
            basicMeasurementUnitMapper.updateByPrimaryKey(basicMeasurementUnit);
        } else {
            basicMeasurementUnit.setCtime(new Date());
            basicMeasurementUnitMapper.insert(basicMeasurementUnit);
        }
        return new Response<String>(ResultEnum.SUCCESS);
    }

    @Override
    public Response<String> del(Integer id) {
        basicMeasurementUnitMapper.deleteByPrimaryKey(id);
        return new Response<String>(ResultEnum.SUCCESS);
    }

    
    @Override
    public LayTableResponse<MeasurementUnitInfo> selectData(String name, Integer companyId, Integer start, Integer pageSize) {
        PageHelper.startPage(start, pageSize);
        List<MeasurementUnitInfo> list = basicMeasurementUnitMapper.selectData(name, companyId, start, pageSize);
        return new LayTableResponse<MeasurementUnitInfo>(list);
    }
    /**
     * 获取所有Unit信息
     */
    @Override
    public List<MeasurementUnit> getAllUnit() {
        return this.basicMeasurementUnitMapper.selectAll();
    }
}
